package com.hup.vd.util;

import com.alibaba.fastjson.JSONObject;
import com.hup.utils.commons.exception.AppCommonException;

import java.net.URL;

/**
 * @author hugan
 * @date 2022/8/17
 */
public abstract class CommonUtil {

    public static String getJSONObjectString(JSONObject jsonObject, String[] keys) {
        for (int j = 0; j < keys.length - 1; j++) {
            jsonObject = (JSONObject) jsonObject.get(keys[j]);
        }
        return jsonObject.getString(keys[keys.length - 1]);
    }

    /**
     * 去掉url的path内容
     *
     * @param url 网页地址, 如"https://www.bb/aa/xx.html"
     * return 如"https://www.bb"
     */
    public static String getWebHost(String url) {
        try {
            URL temp = new URL(url);
            return new URL(temp.getProtocol(), temp.getHost(), temp.getPort(), "").toString();
        } catch (Exception e) {
            throw new AppCommonException("url解析异常:" + url);
        }
    }

}
